package com.xjtu.conan.controller;


import com.xjtu.conan.entity.TheatreInfo;
import com.xjtu.conan.entity.UserInfo;
import com.xjtu.conan.response.AjaxResponse;
import com.xjtu.conan.service.TheatreInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * <p>
 * 剧场版表 前端控制器
 * </p>
 *
 * @author 一只黑猩猩
 * @since 2021-04-19
 */
@CrossOrigin
@RestController
@RequestMapping("/conan/theatre-info")
@Api(value = "剧场版管理模块", description = "这是剧场版信息CRUD的模块")
public class TheatreInfoController {

    @Autowired
    private TheatreInfoService theatreInfoService;

    @ApiOperation(value = "获取全部剧场版", notes = "获取所有剧场版的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getAllTheatres", method = RequestMethod.GET)
    public AjaxResponse getAllTheatres(@RequestParam(value = "current") Integer current,
                                    @RequestParam(value = "size") Integer size) {

        return AjaxResponse.success(theatreInfoService.getAllTheatreInfo(current, size));
    }

    @ApiOperation(value = "添加剧场版", notes = "添加一个剧场版")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tName", value = "剧场版名称", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "剧场版类型(国语1日语0)", required = true, paramType = "form", dataType = "int"),
            @ApiImplicitParam(name = "number", value = "剧场版集数", required = true, paramType = "form", dataType = "int"),
            @ApiImplicitParam(name = "video", value = "剧场版文件", required = true, paramType = "form", dataType = "file")
    })
    @RequestMapping(value = "addTheatre", method = RequestMethod.POST)
    public AjaxResponse addTheatre(@RequestParam(value = "tName") String tName,
                                @RequestParam(value = "type") Integer type,
                                @RequestParam(value = "number") Integer number,
                                @RequestParam(value = "video") MultipartFile video,
                                HttpServletRequest request) throws IOException {
        UserInfo loginUser = (UserInfo) request.getSession().getAttribute("loginUser");
        TheatreInfo theatre = new TheatreInfo()
                .setTheatreName(tName)
                .setTheatreType(type)
                .setTheatreNumber(number)
                .setCreateUser(1384057153556135937L)
//                .setCreateUser(loginUser.getUid())
                .setUpdateUser(1384057153556135937L);
//                .setUpdateUser(loginUser.getUid());
        return AjaxResponse.success("新增剧场版ID->" + theatreInfoService.addTheatreInfo(video, theatre));
    }

    @ApiOperation(value = "更新剧场版", notes = "录入新的剧场版信息并且修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tName", value = "剧场版名称", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "剧场版类型(国语1日语0)", required = true, paramType = "form", dataType = "int"),
            @ApiImplicitParam(name = "number", value = "剧场版集数", required = true, paramType = "form", dataType = "int"),
            @ApiImplicitParam(name = "video", value = "剧场版文件", required = true, paramType = "form", dataType = "file")
    })
    @RequestMapping(value = "updateTheatre", method = RequestMethod.PUT)
    public AjaxResponse updateTheatre(@RequestParam(value = "tName") String tName,
                                @RequestParam(value = "type") Integer type,
                                @RequestParam(value = "number") Integer number,
                                @RequestParam(value = "video") MultipartFile video,
                                HttpServletRequest request) throws IOException {
        UserInfo loginUser = (UserInfo) request.getSession().getAttribute("loginUser");
        TheatreInfo theatre = new TheatreInfo()
                .setTheatreName(tName)
                .setTheatreType(type)
                .setTheatreNumber(number)
                .setUpdateUser(loginUser.getUid());
        return AjaxResponse.success(theatreInfoService.updateTheatreInfo(video, theatre));
    }

    @ApiOperation(value = "删除剧场版信息", notes = "从数据库中删除某个剧场版")
    @ApiImplicitParam(name = "tid", value = "删除剧场版id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/deleteTheatre", method = RequestMethod.DELETE)
    public AjaxResponse deleteTheatre(@RequestParam(value = "tid") Long tid) {
        return AjaxResponse.success("数据库受影响行数->" + theatreInfoService.deleteTheatreInfo(tid));
    }

    @ApiOperation(value = "剧场版总数量", notes = "获取数据库中剧场版总数量")
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    public AjaxResponse getTotal() {
        return AjaxResponse.success(theatreInfoService.getTotal());
    }
}

